package com.shujia.wyh.day04.homework;

/*
    冒泡排序
 */
public class Test1 {
    public static void main(String[] args) {
        //定义一个数组，进行静态初始化
        int[] arr = {32, 43, 6, 78, 123, 43, 87};
        System.out.println("排序之前：");
        printArray(arr);
        //定义一个方法进行冒泡排序
        int[] arr2 = BubblingSort(arr);
        System.out.println("冒泡排序之后：");
        printArray(arr2);

    }


    /**
     * 返回值类型：int[]
     * 参数列表：int[] array
     */
    public static int[] BubblingSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) { //外层for循环控制的是轮次
            System.out.println("=============当前是第"+(i+1)+"次排序=============");
            for (int j = 0; j < array.length - i - 1; j++) { //内层for循环进行相邻的两两比较交换
                if (array[j] > array[j + 1]) {
                    //交换
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
            printArray(array);
        }
        System.out.println("===================================");

        return array;
    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
//            System.out.println(arr[i]);
            if (i == 0) {
                System.out.print("[" + arr[i] + ",");
            } else if (i == arr.length - 1) {
                System.out.println(arr[i] + "]");
            } else {
                System.out.print(arr[i] + ",");
            }
        }
    }
}
